<?php
require_once(FWK_EXCEPTION."CustonException.class.php");
require_once(BIB_ACTIVE_RECORD);
require_once(FWK_MODEL."AbsModelDao.class.php");
require_once(FWK_MODEL."Upload.class.php");
require_once(FWK_UTIL."FormataDatas.class.php");
/**
 * Classe modelo para a estrutura da sessão do noticia que estiver logado
 *
 * @author André Coura <andreccls@gmail.com>
 * @since 1.0 - 19/02/2012
 */
class Post extends AbsModelDao{

	public $_table = "blog_post";

	public $_id = "id_post";

	public function getListaPosts(){
		$strQuery = "SELECT
							post.id_post, post.titulo_post, post.img_post, post.img1_post, post.img2_post, post.img3_post, post.img4_post, 
							post.desc_post, post.data_alteracao, post.id_usuario, 
							(SELECT count(ap.id_comentario) FROM blog_comentario ap
								WHERE ap.id_post = post.id_post AND ap.status_comentario = 'A') as aprov,
							(SELECT count(nok.id_comentario) FROM blog_comentario nok
								WHERE nok.id_post = post.id_post AND nok.status_comentario <> 'A') as naoAprov
						FROM 
							".$this->_table." post";
		$arrRet = ControlDB::getAll($strQuery,3);
		return $arrRet;
	}
	
	/**
	 * Busca dos posts para a Home 
	 * De Acordo com a tala, são apenas 4 posts que irão aparecer.
	 */
	public function getPosts(){
		$strQuery = "SELECT
							id_post, titulo_post, img_post, img1_post, img2_post, img3_post, img4_post,
							desc_post, data_alteracao, id_usuario 
					 FROM 
					 		".$this->_table."
					 WHERE
					 	status_post = 'S'
					 LIMIT
					 	0,4";
		$arrRet = ControlDB::getAll($strQuery,3);
		return $arrRet;
	}
	
	/**
	 * Busca post específico 
	 */
	public function getPost($idPost){
		$strQuery = "SELECT
							id_post, titulo_post, img_post, img1_post, img2_post, img3_post, img4_post, 
							desc_post, data_alteracao, id_usuario, data_post 
					 FROM 
					 		".$this->_table."
					 WHERE
					 	status_post = 'S' AND id_post = '".$idPost."' ";
		
		$arrRet = ControlDB::getRow($strQuery,3);
		return $arrRet;
	}
	
	
	public function getListaPostsPaginada($iniPost=0, $numPosts=10, $mesData=null, $anoData=null, $categoria=null, $busca=null){
		$strQuery = "SELECT
							post.id_post, post.titulo_post, post.img_post, post.img1_post, post.img2_post, post.img3_post, post.img4_post, 
							post.desc_post, post.data_alteracao, post.id_usuario, post.data_post,
							(SELECT count(ap.id_comentario) FROM blog_comentario ap
								WHERE ap.id_post = post.id_post AND ap.status_comentario = 'A') as comentarios
					 FROM 
					 		".$this->_table." post
					 WHERE
					 		post.status_post = 'S'";
			 
		if(isset($mesData) && trim($mesData) !=""){
			$strQuery .= " AND MONTH(post.data_post) = '".$mesData."'";
		}
		if(isset($anoData) && trim($anoData) !=""){
			$strQuery .= " AND YEAR(post.data_post) = '".$anoData."'";
		}
		if(isset($categoria) && trim($categoria) !="" && is_numeric($categoria)){
			$strQuery .= " AND post.id_categoria = '".$categoria."'";
		}
		if(isset($busca) && trim($busca) !=""){
			$strQuery .= " AND (LOWER(post.titulo_post) LIKE '%".strtolower($busca)."%' OR ";
			$strQuery .= " LOWER(post.desc_post) LIKE '%".strtolower($busca)."%' ) ";
		}
		$strQuery .= " ORDER BY
					 		post.data_post DESC
					 LIMIT
					 		".$iniPost.",".$numPosts."";
		
		//print("<pre>");
		//die($strQuery);
		
		$arrRet = ControlDB::getAll($strQuery,3);
		return $arrRet;
	}
	
	public function getDatasPosts(){
		$strQuery = "SELECT
							DISTINCT MONTH(DATA_POST) as 'mes', YEAR(DATA_POST) as 'ano'
					 FROM 
					 		".$this->_table." 
					 WHERE
					 		status_post = 'S'
					 ORDER BY
					 		data_post DESC";
		$arrRet = ControlDB::getAll($strQuery,3);
		return $arrRet;
	}
	

	public function getNumRegs($mesData=null, $anoData=null, $categoria=null, $busca=null){
		$strQuery = "SELECT
							count(id_post)
					 FROM 
					 		".$this->_table."post
					 WHERE
					 		post.status_post = 'S'";
			 
		if(isset($mesData) && trim($mesData) !=""){
			$strQuery .= " AND MONTH(post.data_post) = '".$mesData."'";
		}
		if(isset($anoData) && trim($anoData) !=""){
			$strQuery .= " AND YEAR(post.data_post) = '".$anoData."'";
		}
		if(isset($categoria) && trim($categoria) !="" && is_numeric($categoria)){
			$strQuery .= " AND post.id_categoria = '".$categoria."'";
		}
		if(isset($busca) && trim($busca) !=""){
			$strQuery .= " AND (LOWER(post.titulo_post) LIKE '%".strtolower($busca)."%' OR ";
			$strQuery .= " LOWER(post.desc_post) LIKE '%".strtolower($busca)."%' ) ";
		}
		$arrRet = ControlDB::getRow($strQuery,3);
		return (int)$arrRet[0];
	}

	public function alterar($id=null,$post,$file){
		try{
			self::salvaPostAutoUtf8($post);
			$this->id_post = $id;
			
			/**
			 * UPLOAD DO ARQUIVO
			 */
			parent::uploadArquivoFwk("img_post", $this->id_post, $file, PASTA_UPLOADS_ARQUIVOS."blog/");
			parent::uploadArquivoFwk("img1_post", $this->id_post, $file, PASTA_UPLOADS_ARQUIVOS."blog/");
			parent::uploadArquivoFwk("img2_post", $this->id_post, $file, PASTA_UPLOADS_ARQUIVOS."blog/");
			parent::uploadArquivoFwk("img3_post", $this->id_post, $file, PASTA_UPLOADS_ARQUIVOS."blog/");
			parent::uploadArquivoFwk("img4_post", $this->id_post, $file, PASTA_UPLOADS_ARQUIVOS."blog/");
			
			if(isset($post["data_post"]) && $post["data_post"] != "") {
				$this->data_post = FormataDatas::parseDataSql($post["data_post"]);
			}
			
			//campos default para todos os daos (historico de edição de arquivos)
			$this->data_alteracao = date("Y-m-d H:i:s");
			$this->id_usuario = parent::getUsuarioSessao()->getIdUsuario();
			self::replace();
			if(self::ErrorMsg()){
				print("<pre>");
				print_r($post);
				print_r($file);
				die("<br/><br /><h1>".self::ErrorMsg()."</h1>");
			}
		}catch(DaoException $e){
			throw new CustonException($e->getMensagem());
		}
	}
}
?>